package com.walt.zhong.sample

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import com.walt.zhong.common.webview.IWebViewService
import com.walt.zhong.gaosiblur.BlurImageView
import java.lang.Exception
import java.util.ServiceLoader

class MainActivity : AppCompatActivity() {
    private var bitmap:Bitmap? = null
    private lateinit var blurImageView: BlurImageView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        blurImageView = findViewById(R.id.blurImageView)
        bitmap = BitmapFactory.decodeResource(resources,R.drawable.pics,null)
        bitmap?.let {
            blurImageView.setImageBitmap(it)

        }
    }

    override fun onResume() {
        super.onResume()
        blurImageView.onResume()
    }

    override fun onPause() {
        super.onPause()
        blurImageView.onPause()
    }

    override fun onDestroy() {
        super.onDestroy()
        bitmap?.recycle()
    }

    fun startWebView(view: View) {
        val webviewService = load(IWebViewService::class.java)
        Log.d("walt-zhong","webviewService: $webviewService")
        webviewService?.run {
            ///https://tapi.nolovr.com:12007/index
            startWebViewActivity(this@MainActivity,"https://www.baidu.com","搜一下",true)
            //openLocalWebPage(this@MainActivity)
        }
    }

    fun <S> load(service: Class<S>): S? {
        try {
            return ServiceLoader.load(service).iterator().next()
        } catch (e: Exception) {
            return null
        }
    }
}